package org.jeecg.modules.demo.pubuyorder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.dto.message.TemplateMessageDTO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.demo.entity.PuContract;
import org.jeecg.modules.demo.demo.service.IPuContractService;
import org.jeecg.modules.demo.expenseAllocation.entity.FeiYongVo;
import org.jeecg.modules.demo.expenseAllocation.entity.YxExpenseAllocation;
import org.jeecg.modules.demo.puInvoice.entity.PuInvoice;
import org.jeecg.modules.demo.pubuyorder.entity.PuBuyOrder;
import org.jeecg.modules.demo.pubuyorder.service.IPuBuyOrderService;
import org.jeecg.modules.demo.pusettlement.entity.PuSettlement;
import org.jeecg.modules.demo.qualityOrder.service.IPuQualityOrderService;
import org.jeecg.modules.demo.sopayorder.service.ISoPayorderService;
import org.jeecg.modules.demo.utils.HttpClientUtil;
import org.jeecg.modules.demo.utils.ReflectUtil;
import org.jeecg.modules.demo.utils.generateOddUtil;
import org.jeecg.modules.demo.warehouseorder.service.IWarehouseOrderService;
import org.jeecg.modules.demo.yxqcyfOrder.entity.YxQcyfOrder;
import org.jeecg.modules.demo.yxqcyfOrder.service.IYxQcyfOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

 /**
 * @Description: pu_buy_order
 * @Author: jeecg-boot
 * @Date:   2021-11-24
 * @Version: V1.0
 */
@Api(tags="pu_buy_order")
@RestController
@RequestMapping("/pubuyorder/puBuyOrder")
@Slf4j
public class PuBuyOrderController extends JeecgController<PuBuyOrder, IPuBuyOrderService> {
	 @Autowired
	 private IPuBuyOrderService puBuyOrderService;
	 @Autowired
	 private IPuQualityOrderService puQualityOrderService;
	 @Autowired
	 private IPuContractService puContractService;
	 @Autowired
	 private ISoPayorderService soPayorderService;
	 @Autowired
	 private IWarehouseOrderService warehouseOrderService;
	 @Autowired
	 private IYxQcyfOrderService yxQcyfOrderService;
	 @Autowired
	 private ISysBaseAPI sysBaseAPI;


	 @Value(value = "${logisticsInformationUrl}")
	 private String logisticsInformationUrl;

	 private static final String TITLE = "单据审核";



	 private String getMessage(String name,String date) {
		 String str ="由【"+name+"】于【"+date+"】发起的采购订单需要您的审核，请及时查看！";
		 return str;
	 }


	/**
	 * 分页列表查询
	 *
	 * @param puBuyOrder
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "采购订单-分页列表查询")
	@ApiOperation(value="采购订单-分页列表查询", notes="采购订单-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="yunxiao/pubuyorder/PuBuyOrderList")
	public Result<?> queryPageList(PuBuyOrder puBuyOrder,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		try{
			QueryWrapper<PuBuyOrder> queryWrapper = QueryGenerator.initQueryWrapper(puBuyOrder, req.getParameterMap());
			queryWrapper.ne("SHANCHU",1);
			queryWrapper.eq("DOCUMENT_TYPE",0);
			if(ReflectUtil.isObjectNull(puBuyOrder)){
				queryWrapper.ne("SHENHE",2);
			}
			Page<PuBuyOrder> page = new Page<PuBuyOrder>(pageNo, pageSize);
			IPage<PuBuyOrder> pageList = puBuyOrderService.page(page, queryWrapper);
			return Result.OK(pageList);
		}catch (Exception e){
			log.error(e.getMessage(), e);
			return Result.error(500,"网络故障！");
		}
	}

	 /**
	  * 期初采购订单分页列表查询
	  *
	  * @param puBuyOrder
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "期初采购订单-分页列表查询")
	 @ApiOperation(value="期初采购订单-分页列表查询", notes="期初采购订单-分页列表查询")
	 @GetMapping(value = "/queryQcPageList")
	 @PermissionData(pageComponent="yunxiao/pubuyorder/periodPuBuyOrderList")
	 public Result<?> queryQcPageList(PuBuyOrder puBuyOrder,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 try{
			 QueryWrapper<PuBuyOrder> queryWrapper = QueryGenerator.initQueryWrapper(puBuyOrder, req.getParameterMap());
			 queryWrapper.ne("SHANCHU",1);
			 queryWrapper.eq("DOCUMENT_TYPE",1);
			 if(ReflectUtil.isObjectNull(puBuyOrder)){
				 queryWrapper.ne("SHENHE",2);
			 }
			 Page<PuBuyOrder> page = new Page<PuBuyOrder>(pageNo, pageSize);
			 IPage<PuBuyOrder> pageList = puBuyOrderService.page(page, queryWrapper);
			 return Result.OK(pageList);
		 }catch (Exception e){
			 log.error(e.getMessage(), e);
			 return Result.error(500,"网络故障！");
		 }
	 }


	 /**
	  * 审核订单
	  * @param puBuyOrder
	  * @param req
	  * @return
	  */
	@AutoLog(value = "采购订单-审核")
	@ApiOperation(value="采购订单-审核", notes="采购订单-审核")
	@PostMapping(value = "/checkUrl")
	public Result<?> checkUrl(@RequestBody PuBuyOrder puBuyOrder,HttpServletRequest req){
		try{
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			if(!sysUser.getId().equals(puBuyOrder.getCheckmen())){
				return Result.error(500,"您不是当前单据的指定审核人！");
			}
			if(puBuyOrder.getPoStaff()==null||puBuyOrder.getDepartment()==null||puBuyOrder.getProject()==null){
				return  Result.error(500,"请先完善订单信息！");
			}
			if(puBuyOrder.getShenhe()==2){
				return Result.error("订单已关闭！");
			}
			if(puBuyOrder.getShenhe()==1){
				return Result.error(500,"订单已审核，无需重复审核！");
			}
			puBuyOrder.setCheckmen(sysUser.getId());
			puBuyOrder.setShenhe(1);
			puBuyOrder.setShTime(new Date());

			puBuyOrderService.updateById(puBuyOrder);
			return  Result.OK("审核成功!");
		}catch (Exception e){
			log.error(e.getMessage(), e);
			return Result.error(500,"系统错误！");
		}
	}

	 /**
	  * 弃审
	  * @param puBuyOrder
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "采购订单-弃审")
	 @ApiOperation(value="采购订单-弃审", notes="采购订单-弃审")
	 @PostMapping(value = "/abandontrialUrl")
	 public Result<?> abandontrialUrl(@RequestBody PuBuyOrder puBuyOrder,HttpServletRequest req){
		 try{
			 // 获取登录用户信息
			 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			 if(!sysUser.getId().equals(puBuyOrder.getCheckmen())){
				 return Result.error(500,"您不是当前单据的指定审核人！");
			 }
			 if(puBuyOrder.getShenhe()==2){
				 return Result.error(300,"订单已关闭！");
			 }
			 if(puBuyOrder.getShenhe()==0){
				 return Result.error(300,"请先审核订单！");
			 }
			 if(puBuyOrder.getDocumentType()!=1){
				 if(puBuyOrder.getInvoice()==1){
					 return Result.error(300,"已生成预开发票，无法弃审！");
				 }
				 List<String> ids = puQualityOrderService.selectByPoOrder(puBuyOrder.getPoOrder());
				 if(!ids.isEmpty()){
					 return Result.error(300,"已生成质检单，无法弃审！");
				 }
			 }else{
			 	if(puBuyOrder.getOdlx()==2){
					List<YxQcyfOrder> qcyfOrders = yxQcyfOrderService.getListByPoOrder(puBuyOrder.getPoOrder());
					if(!qcyfOrders.isEmpty()){
						return Result.error(300,"已生成对应的期初应付款，无法弃审！");
					}
				}else{
					List<String> ids = puQualityOrderService.selectByPoOrder(puBuyOrder.getPoOrder());
					if(!ids.isEmpty()){
						return Result.error(300,"已生成质检单，无法弃审！");
					}
				}

			 }
			 puBuyOrder.setShTime(null);
			 puBuyOrder.setShenhe(0);
			 puBuyOrderService.updateById(puBuyOrder);
			 return  Result.OK("弃审成功!");
		 }catch (Exception e){
			 log.error(e.getMessage(), e);
		 	 return Result.error(500,"系统错误！");
		 }
	 }
	 /**
	  * 关闭订单
	  * @param puBuyOrder
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "采购订单-关闭订单")
	 @ApiOperation(value="采购订单-关闭订单", notes="采购订单-关闭订单")
	 @PostMapping(value = "/closeOrder")
	 public Result<?> closeOrder(@RequestBody PuBuyOrder puBuyOrder,HttpServletRequest req){
		 try{
			 if(puBuyOrder.getShenhe()==2){
				 return Result.error("订单已关闭！");
			 }
			 if(puBuyOrder.getShenhe()==0){
				 return Result.error("请先审核订单！");
			 }
			 puBuyOrder.setShTime(new Date());
			 puBuyOrder.setShenhe(2);
			 puBuyOrderService.updateById(puBuyOrder);
			 return  Result.OK("订单已关闭!");
		 }catch (Exception e){
			 log.error(e.getMessage(), e);
			 return Result.error(500,"系统错误！");
		 }
	 }

	 /**
	  * 开启订单
	  * @param puBuyOrder
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "采购订单-开启订单")
	 @ApiOperation(value="采购订单-开启订单", notes="采购订单-开启订单")
	 @PostMapping(value = "/openOrder")
	 public Result<?> openOrder(@RequestBody PuBuyOrder puBuyOrder,HttpServletRequest req){
		 try{
			 if(puBuyOrder.getShenhe()==1||puBuyOrder.getShenhe()==0){
				 return Result.error("订单已是开启状态！");
			 }
			 puBuyOrder.setShTime(new Date());
			 puBuyOrder.setShenhe(1);
			 puBuyOrderService.updateById(puBuyOrder);
			 return  Result.OK("订单已开启!");
		 }catch (Exception e){
			 log.error(e.getMessage(), e);
			 return Result.error(500,"系统错误！");
		 }
	 }
	/**
	 *   添加
	 * @param puBuyOrder
	 * @return
	 */
	@AutoLog(value = "期初采购订单-添加")
	@ApiOperation(value="期初采购订单-添加", notes="期初采购订单-添加")
	@PostMapping(value = "/addQcOrder")
	public Result<?> addQcOrder(@RequestBody PuBuyOrder puBuyOrder,HttpServletRequest req) {
		try{
			String tenantId = req.getHeader("tenant-id");
			String precode = puBuyOrderService.getTeantCodeById(Integer.parseInt(tenantId)).toUpperCase();
			String str = puBuyOrderService.getMaxCode();
			Map<String,String> map = new HashMap<>();
			map.put("maxCode",str==null?null:str);
			String name = precode+"QCCG_";
			String resultCode = generateOddUtil.createCode(map,name);
			puBuyOrder.setPoOrder(resultCode);
			puBuyOrder.setShanchu(0);
			puBuyOrder.setShenhe(0);
			puBuyOrder.setZhijian(0);
			puBuyOrder.setCreateTime(new Date());
			puBuyOrder.setDocumentType(1);
			Double d = 0.000;
			puBuyOrder.setYkje(new BigDecimal(d));
			puBuyOrder.setYktotal(new BigDecimal(d));
			puBuyOrder.setZjtotal(puBuyOrder.getOdlx()==1?new BigDecimal(d):puBuyOrder.getPoTotail());
			puBuyOrder.setYhxTotail(new BigDecimal(d));
			puBuyOrder.setYhxMoney(new BigDecimal(d));
			puBuyOrder.setInvoice(0);
			puBuyOrderService.save(puBuyOrder);
			return Result.OK("添加成功！");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	 @PostMapping(value = "/getYWHomePageData")
	 public String getYWHomePageData(HttpServletRequest req){
		 Gson gson = new Gson();
		 Map map = new HashMap();
		 String tenantId = req.getHeader("tenant-id");
		 try{
			 Map<String,Double> buyOrder = puBuyOrderService.getNumAndMoeny();
			 Map<String,Double> saleOrder = puBuyOrderService.getSaleOrderNumByMoney();
			 Map<String,Double> cgzjData = puBuyOrderService.getCgYearData(tenantId);
			 Map<String,Double> xszjData = puBuyOrderService.getXsYearData();
			 map.put("cgYearsl",cgzjData.get("CGSL"));
			 map.put("xsYearsl",xszjData.get("XSNUM"));
			 map.put("cgYearje",cgzjData.get("CGMONEY"));
			 map.put("xsYearje",xszjData.get("XSMONEY"));
			 map.put("cgNum",buyOrder.get("CGSL"));
			 map.put("cgMoney",buyOrder.get("CGMONEY"));
			 map.put("xsNum",saleOrder.get("XSNUM"));
			 map.put("xsMoney",saleOrder.get("XSMONEY"));
			 String json = gson.toJson(map);
			 return json;
		 }catch (Exception e){
			 e.printStackTrace();
			 map.put("code",500);
			 map.put("message","系统错误！");
			 String json = gson.toJson(map);
			 return json;
		 }
	 }
	 @PostMapping(value = "/getJYHomePageData")
	 public String getJYHomePageData(){
		 Gson gson = new Gson();
		 Map map = new HashMap();
		 try{
			 Map<String,Double> payOrder = soPayorderService.getNumAndMoney();
			 Map<String,Double> sktOrder = soPayorderService.getSktOrderNumAndMoney();
			 Map<String,Double> cgfpData = puBuyOrderService.getFkYearData();
			 Map<String,Double> xsfpData = puBuyOrderService.getSkYearData();
			 map.put("cgYearfpsl",cgfpData.get("CGFPSL"));
			 map.put("xsYearfpsl",xsfpData.get("XSFPSL"));
			 map.put("cgYearfpje",cgfpData.get("CGFPJE"));
			 map.put("xsYearfpje",xsfpData.get("XSFPJE"));
			 map.put("fkNum",payOrder.get("CGFPSL"));
			 map.put("fkMoney",payOrder.get("CGFPJE"));
			 map.put("skNum",sktOrder.get("XSFPSL"));
			 map.put("skMoney",sktOrder.get("XSFPJE"));
			 String json = gson.toJson(map);
			 return json;
		 }catch (Exception e){
			 e.printStackTrace();
			 map.put("code",500);
			 map.put("message","系统错误！");
			 String json = gson.toJson(map);
			 return json;
		 }
	 }

	 @PostMapping(value = "/getYWMonthPageData")
	 public String getYWMonthPageData(HttpServletRequest req){
		 Gson gson = new Gson();
		 Map map = new HashMap();
		 String tenantId = req.getHeader("tenant-id");
		 try{
			 Map<String,Double> cgMonthData = puBuyOrderService.getCgMonthData();
			 Map<String,Double> xsMonthData = puBuyOrderService.getXsMonthData();
			 Map<String,Double> cgzjData = puBuyOrderService.getCgYearData(tenantId);
			 Map<String,Double> xszjData = puBuyOrderService.getXsYearData();
			 map.put("cgYearsl",cgzjData.get("CGSL"));
			 map.put("xsYearsl",xszjData.get("XSNUM"));
			 map.put("cgYearje",cgzjData.get("CGMONEY"));
			 map.put("xsYearje",xszjData.get("XSMONEY"));
			 map.put("cgNum",cgMonthData.get("CGSL"));
			 map.put("cgMoney",cgMonthData.get("CGMONEY"));
			 map.put("xsNum",xsMonthData.get("XSNUM"));
			 map.put("xsMoney",xsMonthData.get("XSMONEY"));
			 String json = gson.toJson(map);
			 return json;
		 }catch (Exception e){
			 e.printStackTrace();
			 map.put("code",500);
			 map.put("message","系统错误！");
			 String json = gson.toJson(map);
			 return json;
		 }
	 }

	 @PostMapping(value = "/getJYMonthPageData")
	 public String getJYMonthPageData(){
		 Gson gson = new Gson();
		 Map map = new HashMap();
		 try{
			 Map<String,Double> fkMonthData = puBuyOrderService.getFkMonthData();
			 Map<String,Double> skMonthData = puBuyOrderService.getSkMonthData();
			 Map<String,Double> cgfpData = puBuyOrderService.getFkYearData();
			 Map<String,Double> xsfpData = puBuyOrderService.getSkYearData();
			 map.put("cgYearfpsl",cgfpData.get("CGFPSL"));
			 map.put("xsYearfpsl",xsfpData.get("XSFPSL"));
			 map.put("cgYearfpje",cgfpData.get("CGFPJE"));
			 map.put("xsYearfpje",xsfpData.get("XSFPJE"));
			 map.put("fkNum",fkMonthData.get("CGFPSL"));
			 map.put("fkMoney",fkMonthData.get("CGFPJE"));
			 map.put("skNum",skMonthData.get("XSFPSL"));
			 map.put("skMoney",skMonthData.get("XSFPJE"));
			 String json = gson.toJson(map);
			 return json;
		 }catch (Exception e){
			 e.printStackTrace();
			 map.put("code",500);
			 map.put("message","系统错误！");
			 String json = gson.toJson(map);
			 return json;
		 }
	 }
	 @PostMapping(value = "/getYWYearPageData")
	 public String getYWYearPageData(HttpServletRequest req){
		 Gson gson = new Gson();
		 Map map = new HashMap();
		 String tenantId = req.getHeader("tenant-id");
		 try{
			 Map<String,Double> cgzjData = puBuyOrderService.getCgYearData(tenantId);
			 Map<String,Double> xszjData = puBuyOrderService.getXsYearData();
			 map.put("cgYearsl",cgzjData.get("CGSL"));
			 map.put("xsYearsl",xszjData.get("XSNUM"));
			 map.put("cgYearje",cgzjData.get("CGMONEY"));
			 map.put("xsYearje",xszjData.get("XSMONEY"));
			 map.put("cgNum",cgzjData.get("CGSL"));
			 map.put("cgMoney",cgzjData.get("CGMONEY"));
			 map.put("xsNum",xszjData.get("XSNUM"));
			 map.put("xsMoney",xszjData.get("XSMONEY"));
			 String json = gson.toJson(map);
			 return json;
		 }catch (Exception e){
			 e.printStackTrace();
			 map.put("code",500);
			 map.put("message","系统错误！");
			 String json = gson.toJson(map);
			 return json;
		 }
	 }
	 @PostMapping(value = "/getJYYearPageData")
	 public String getJYYearPageData(){
		 Gson gson = new Gson();
		 Map map = new HashMap();
		 try{
			 Map<String,Double> cgfpData = puBuyOrderService.getFkYearData();
			 Map<String,Double> xsfpData = puBuyOrderService.getSkYearData();
			 map.put("cgYearfpsl",cgfpData.get("CGFPSL"));
			 map.put("xsYearfpsl",xsfpData.get("XSFPSL"));
			 map.put("cgYearfpje",cgfpData.get("CGFPJE"));
			 map.put("xsYearfpje",xsfpData.get("XSFPJE"));
			 map.put("fkNum",cgfpData.get("CGFPSL"));
			 map.put("fkMoney",cgfpData.get("CGFPJE"));
			 map.put("skNum",xsfpData.get("XSFPSL"));
			 map.put("skMoney",xsfpData.get("XSFPJE"));
			 String json = gson.toJson(map);
			 return json;
		 }catch (Exception e){
			 e.printStackTrace();
			 map.put("code",500);
			 map.put("message","系统错误！");
			 String json = gson.toJson(map);
			 return json;
		 }
	 }


	 @PostMapping(value = "/getTeantName")
	 public String getTeantName(HttpServletRequest req){
		 Gson gson = new Gson();
		 Map map = new HashMap();
		 try{
			 String tenantId = req.getHeader("tenant-id");
			 String teantName = puBuyOrderService.getTeantName(Integer.parseInt(tenantId));
			 map.put("code",200);
			 map.put("teantName",teantName);
			 map.put("message","查询成功！");
			 String json = gson.toJson(map);
			 return json;
		 }catch (Exception e){
			 e.printStackTrace();
			 map.put("code",500);
			 map.put("message","系统错误！");
			 String json = gson.toJson(map);
			 return json;
		 }
	 }

	 @PostMapping(value = "/getLogisticsInformation")
	 public String getLogisticsInformation(@RequestBody PuBuyOrder puBuyOrder){
		Gson gson = new Gson();
		 Map map = new HashMap();
		try{
			if(puBuyOrder.getTransport().equals("水路")){
				Map<String, Object> data = new HashMap<>();
				data.put("contractNo", puBuyOrder.getPuConNo());
				//测试数据
//				data.put("contractNo", "test-2023");
				String json = HttpClientUtil.doPostJson(logisticsInformationUrl,data);
				return json;
			}else{
				map.put("code",500);
				map.put("message","该订单运输方式不支持查看物流信息！");
				String json = gson.toJson(map);
				return json;
			}
		}catch (Exception e){
			e.printStackTrace();
			map.put("code",500);
			map.put("message","系统错误！");
			String json = gson.toJson(map);
			return json;
		}
	 }

	 @PostMapping(value = "/getCgPieChatData")
	 public String getCgPieChatData(@RequestParam(name="beginTime",required=false)String beginTime,@RequestParam(name="endTime",required=false)String endTime){
		 Gson gson = new Gson();
		 Map map = new HashMap();
		try{
			Map<String,Double>  orderMap = puBuyOrderService.getOrderQuantity(beginTime,endTime);
			Map<String,Double>  qualityMap = puBuyOrderService.getQualityIQ(beginTime,endTime);
			Map<String,Double>  settlementMap = puBuyOrderService.getSettlementVolume(beginTime,endTime);
			Map<String,Double>  invoiceMap = puBuyOrderService.getInvoiceQuantity(beginTime,endTime);
			map.put("orderQuantity",orderMap.get("ORDERQUANTITY"));
			map.put("qualityIQ",qualityMap.get("QUALITYIQ"));
			map.put("settlementVolume",settlementMap.get("SETTLEMENTVOLUME"));
			map.put("invoiceQuantity",invoiceMap.get("INVOICEQUANTITY"));
			map.put("code",200);
			map.put("message","成功！");
			String json = gson.toJson(map);
			return json;
		}catch (Exception e){
			e.printStackTrace();
			map.put("code",500);
			map.put("message","系统错误！");
			String json = gson.toJson(map);
			return json;
		}
	 }


	 @PostMapping(value = "/getDataList")
	 public String getDataList(@RequestParam(name="str",required=true)String str){
		 Gson gson = new Gson();
		 Map map = new HashMap();
		 try{
			 List<Map<String,Double>> dingdan = puBuyOrderService.getDingDan(str);
			 List<PuSettlement> jiesuan = puBuyOrderService.getJieSuan(str);
			 List<Map<String,Double>> zhijian =puBuyOrderService.getZhiJian(str);
			 List<Map<String,Double>> ruku =puBuyOrderService.getRuKu(str);
			 List<PuInvoice> fapiao =puBuyOrderService.getFaPiao(str);
			 List<PuInvoice> yukai =puBuyOrderService.getYuKaiFaPiao(str);
			 List<Map<String,Double>> fukuan =puBuyOrderService.getFuKuan(str);
			 List<YxExpenseAllocation> yuFuKuan =puBuyOrderService.getYuFuKuan(str);
			 List<FeiYongVo> feiYongVos = puBuyOrderService.getFeiYong(str);
			 map.put("code",200);
			 map.put("message","查询成功！");
			 map.put("dingdan",dingdan);
			 map.put("zhijian",zhijian);
			 map.put("ruku",ruku);
			 map.put("jiesuan",jiesuan);
			 map.put("fapiao",fapiao);
			 map.put("yukai",yukai);
			 map.put("fukuan",fukuan);
			 map.put("yuFuKuan",yuFuKuan);
			 map.put("feiyong",feiYongVos);
			 String json = gson.toJson(map);
			 return json;
		 }catch (Exception e){
			 e.printStackTrace();
			 map.put("code",500);
			 map.put("message","系统错误！");
			 String json = gson.toJson(map);
			 return json;
		 }
	 }

	 /**
	  *  期初采购订单编辑
	  *
	  * @param puBuyOrder
	  * @return
	  */
	 @AutoLog(value = "期初采购订单-编辑")
	 @ApiOperation(value="期初采购订单-编辑", notes="期初采购订单-编辑")
	 @PutMapping(value = "/qcEdit")
	 public Result<?> qcEdit(@RequestBody PuBuyOrder puBuyOrder) {
		 try{
			 Double d = 0.000;
			 puBuyOrder.setYkje(new BigDecimal(d));
			 puBuyOrder.setYktotal(new BigDecimal(d));
			 puBuyOrder.setZjtotal(puBuyOrder.getOdlx()==1?new BigDecimal(d):puBuyOrder.getPoTotail());
			 puBuyOrder.setYhxTotail(new BigDecimal(d));
			 puBuyOrder.setYhxMoney(new BigDecimal(d));
			 puBuyOrder.setInvoice(0);
			 puBuyOrderService.updateById(puBuyOrder);
			 return Result.OK("编辑成功!");
		 }catch (Exception e){
			 e.printStackTrace();
			 return Result.error(500,"系统错误！");
		 }
	 }

	/**
	 *  采购订单完善信息
	 *
	 * @param puBuyOrder
	 * @return
	 */
	@AutoLog(value = "采购订单-完善信息")
	@ApiOperation(value="采购订单-完善信息", notes="采购订单-完善信息")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PuBuyOrder puBuyOrder) {
		try{
			if(puBuyOrder.getHsdj().doubleValue()==0){
				return  Result.error("请完善单据价格信息！");
			}
			Double d = 0.000;
			puBuyOrder.setYkje(new BigDecimal(d));
			puBuyOrder.setYktotal(new BigDecimal(d));
			puBuyOrder.setZjtotal(new BigDecimal(d));
			puBuyOrder.setYhxTotail(new BigDecimal(d));
			puBuyOrder.setYhxMoney(new BigDecimal(d));
			puBuyOrder.setInvoice(0);
			puBuyOrderService.updateById(puBuyOrder);
			LoginUser user = sysBaseAPI.getUserById(puBuyOrder.getPoStaff());
			TemplateMessageDTO templateMessageDTO = new TemplateMessageDTO();
			templateMessageDTO.setFromUser("管理员");
			templateMessageDTO.setToUser(puBuyOrder.getCheckmen()+",");
			templateMessageDTO.setTitle(TITLE);
			templateMessageDTO.setTemplateCode("order");
			Map<String, String> templateParam = new HashMap<>();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String str= simpleDateFormat.format(new Date());
			templateParam.put("ts_date",str);
			templateParam.put("ts_content", getMessage(user!=null?user.getRealname():"业务员不存在！", str));
			templateParam.put("checkOrder",puBuyOrder.getPoOrder());
			templateParam.put("cd_url","/pubuyorder/puBuyOrder/list");
			templateMessageDTO.setTemplateParam(templateParam);
			sysBaseAPI.sendTemplateAnnouncement(templateMessageDTO);
			return Result.OK("完善信息成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "采购订单-通过id删除")
	@ApiOperation(value="采购订单-通过id删除", notes="采购订单-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try{
			PuBuyOrder puBuyOrder = puBuyOrderService.getById(id);
			if(puBuyOrder.getShenhe()==2){
				return Result.error("订单已关闭，无法删除！");
			}
			if(puBuyOrder.getShenhe()==1){
				return Result.error("订单已审核，无法删除！");
			}
			PuContract puContract = puBuyOrderService.getContractByNumber(puBuyOrder.getPuConNo());
			puContract.setStatus("0");
			puContractService.updateById(puContract);
			puBuyOrderService.removeById(puBuyOrder.getId());
			return Result.OK("删除成功!");
		}catch (Exception e){
			log.error(e.getMessage(), e);
			return Result.error(500,"系统错误！");
		}
	}

	 /**
	  *   通过id删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "期初采购订单-通过id删除")
	 @ApiOperation(value="期初采购订单-通过id删除", notes="期初采购订单-通过id删除")
	 @DeleteMapping(value = "/qcOrderDelete")
	 public Result<?> qcOrderDelete(@RequestParam(name="id",required=true) String id) {
		 try{
			 PuBuyOrder puBuyOrder = puBuyOrderService.getById(id);
			 if(puBuyOrder.getShenhe()==2){
				 return Result.error("订单已关闭，无法删除！");
			 }
			 if(puBuyOrder.getShenhe()==1){
				 return Result.error("订单已审核，无法删除！");
			 }
			 puBuyOrderService.removeById(puBuyOrder.getId());
			 return Result.OK("删除成功!");
		 }catch (Exception e){
			 log.error(e.getMessage(), e);
			 return Result.error(500,"系统错误！");
		 }
	 }
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "采购订单-批量删除")
	@ApiOperation(value="采购订单-批量删除", notes="采购订单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.puBuyOrderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "采购订单-通过id查询")
	@ApiOperation(value="采购订单-通过id查询", notes="采购订单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		PuBuyOrder puBuyOrder = puBuyOrderService.getById(id);
		if(puBuyOrder==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(puBuyOrder);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param puBuyOrder
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PuBuyOrder puBuyOrder) {
        return super.exportXls(request, puBuyOrder, PuBuyOrder.class, "pu_buy_order");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PuBuyOrder.class);
    }

}
